//
//  Qb_BillViewController.m
//  Cd_Qb
//
//  Created by mac on 2018/3/9.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "Qb_BillViewController.h"

#import "Qb_BilltTableViewCell.h"
#import "PopoverView.h"


#import "MJRefresh.h"


@interface Qb_BillViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *BilltableView;
@end


static NSString * cellID = @"myBillCell";

@implementation Qb_BillViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"账单明细";
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame= CGRectZero;
    [btn setImage:[UIImage imageNamed:@"Qb_Blil_Right"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(RightClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem*RightBtn = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem = RightBtn;
   
    [self.view addSubview:self.BilltableView];
    
    self.BilltableView.separatorStyle = UITableViewCellSelectionStyleNone;
    self.BilltableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    // Do any additional setup after loading the view.
}

#pragma mark -- Action
//点击右边
-(void)RightClick:(UIButton *)sender{
        NSMutableArray *lastary = [NSMutableArray array];
        NSArray *ary = @[@"用户消费",@"用户升级",@"信用卡消费",@"信用卡还款",@"信用卡充值",@"提款",@"代还提款",@"全部"];
        for (int i = 0 ; i<ary.count; i++) {
            PopoverAction *action = [PopoverAction actionWithTitle:ary[i] handler:^(PopoverAction *action) {
                NSLog(@"%@",action.title);
            }];
            [lastary addObject:action];
        }
        PopoverView *popoverView = [PopoverView popoverView];
        popoverView.style = PopoverViewStyleDefault;
        popoverView.showShade = YES;
        // 在没有系统控件的情况下调用可以使用显示在指定的点坐标的方法弹出菜单控件.
        [popoverView showToPoint:CGPointMake(ScreenW - 20*AdaptiveScale_W, NavBarHeight) withActions:lastary];
}

-(void)loadNewData{
    //这里假设2秒之后获取到了最新的数据，刷新tableview，并且结束刷新控件的刷新状态
    __weak typeof(self) weakSelf = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //刷新列表
        [weakSelf.BilltableView reloadData];
        //拿到当前的刷新控件，结束刷新状态
        [weakSelf.BilltableView.mj_header endRefreshing];
    });
}

-(void)loadMoreData{
    //这里假设2秒之后获取到了更多的数据，刷新tableview，并且结束刷新控件的刷新状态
    __weak typeof(self) weakSelf = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //刷新列表
        [weakSelf.BilltableView reloadData];
        //拿到当前的刷新控件，结束刷新状态
        [weakSelf.BilltableView.mj_footer endRefreshing];
    });
}

#pragma mark --UITableViewDelegate&&UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    Qb_BilltTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    [cell loadData:@[]];
//    return cell;
    
    QB_MyBillCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (!cell)
    {
        cell = [QB_MyBillCell myBillCell];
        
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90*AdaptiveScale_W;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%ld ----",indexPath.row);
    
    Qb_DetailOfTheBillViewController * con = [[Qb_DetailOfTheBillViewController alloc] initWithTitle:@"账单详情" andType:@""];
    con.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:con animated:YES];
}



-(UITableView *)BilltableView{
    if (!_BilltableView) {
        _BilltableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
       _BilltableView.dataSource = self;
        _BilltableView.delegate = self;
        _BilltableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
        _BilltableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
        _BilltableView.showsVerticalScrollIndicator = NO;
        [_BilltableView registerNib:[UINib nibWithNibName:@"Qb_BilltTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        WS(weakself);
        _BilltableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakself loadNewData];
        } ];
        
        _BilltableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            //刷新时候，需要执行的代码。一般是请求更多数据，请求成功之后，刷新列表
            [weakself loadMoreData];
        }];
    }
    return _BilltableView;
}


@end
